/**
 * @TreeCreator.as
 * @Author - Dheeraj Singh
 * @Version - 1.0
 * @Date - March 25, 2013
 * 
 * Copyright (c) 2013 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package accounts.accountsInformation.components {

import mx.collections.ArrayCollection;
import mx.controls.Alert;
	
public class TreeCreator {
	
	public function TreeCreator() {}
	
		public static function insertchildForNode(node:TreeNodeH, childNode:Object):void {
			if(node.children == null){
				node.children = new ArrayCollection();
			}
			var newChild:TreeNodeH = new TreeNodeH();
			newChild.accountCode = childNode.accountCode;
			newChild.accountName = childNode.accountName;
			newChild.accountDescription = childNode.accountDescription;
			newChild.accountType = childNode.accountType;
			newChild.accountTypeName = childNode.accountTypeName;
			newChild.accountColor = childNode.accountColor;
			newChild.accountNotes = childNode.accountNotes;
			newChild.accountDepth = childNode.accountDepth;
			newChild.parentAccountCode = childNode.parentAccountCode;
			newChild.childCount = childNode.childCount;
			if(childNode.primaryAccount == "Y") {
				newChild.primaryAccount = "Yes";
			} else {
				newChild.primaryAccount = "No";
			}
			newChild.initialBalance = childNode.initialBalance;
			newChild.initialTransferAccount = childNode.initialTransferAccount;
			newChild.balance = childNode.balance;
			newChild.total = childNode.total;
			if(childNode.reconciledFlag == "Y") {
				newChild.reconciledFlag = "Yes";
			} else {
				newChild.reconciledFlag = "No";
			}
			newChild.lastReconciledDate = childNode.lastReconciledDate;
			newChild.lastReconciledBy = childNode.lastReconciledBy;
			newChild.creatorId = childNode.creatorId;
			newChild.modifierId = childNode.modifierId;
			newChild.creationDate = childNode.creationDate;
			newChild.modificationDate = childNode.modificationDate;
			newChild.parentAccount = childNode.parentAccount;
			newChild.childAccount = childNode.childAccount;   
			node.children.addItem(newChild);
		}

		public static function createHierarchicalArrayCollection(flatArrayCollection:ArrayCollection, flag:String):TreeNodeH 
		{	
			var top:TreeNodeH = new TreeNodeH();
			if(flag == "menu"){
				top.accountName = "Set Up";
			}else{
				top.accountName = "Accounts Hierarchy";
			}
			
			top.accountCode = "";
			top.parentAccountCode = "";
			
			var parentNode:TreeNodeH = new TreeNodeH();
			for(var i:int = 0; i<flatArrayCollection.length;i++) {
				var currentRecord:Object = flatArrayCollection.getItemAt(i) as Object;
				if(currentRecord.parentAccountCode == "0"){
					parentNode = top;
				}
				else{
					parentNode = searchTreeForParentNode(currentRecord.parentAccountCode,top);
				}
				if(parentNode != null) {
					insertchildForNode(parentNode,currentRecord);
				}
			}
			return top;
		}
		
		public static function searchTreeForParentNode(parentID:String,rootNode:TreeNodeH):TreeNodeH {
			var openList:ArrayCollection = new ArrayCollection();
			openList.addItem(rootNode);
			var index:int = 0;
			while (openList.length != 0) {
				var node:TreeNodeH = openList.getItemAt(index) as TreeNodeH;
				if(parentID == node.accountCode) {
					return node;
				}
				var nodeChildsList:ArrayCollection = node.children;
				if(nodeChildsList != null) {
					var nLength:int = nodeChildsList.length;
					for(var i:int=0; i<nLength;i++) {
						var currentItem:TreeNodeH = nodeChildsList.getItemAt(i) as TreeNodeH;
						openList.addItem(currentItem);
					}            
				}
				index++;                
			}
			return null;
		}
	}
}

